﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ApplyRootMotion : MonoBehaviour 
{
	//public Transform m_flagObject;	// 用来测试位置的一个对象

	//-- Root Motion 控制变量
	Transform m_rootBone;
    Vector3 m_lastRootPos;
    public Vector3 _两帧之间根骨骼本地坐标差值;
    int m_lastAnimTime;

    public List<string> m_AnimNameList;

    Animation anim;

    CharacterController _cc;

    void Start () 
	{
		//-- 从SkinnedMeshRenderer中读取Root Bone
		SkinnedMeshRenderer skinMesh = this.gameObject.GetComponentInChildren<SkinnedMeshRenderer>();
        m_rootBone = skinMesh.rootBone;

        //-- 变量初始化
        _两帧之间根骨骼本地坐标差值 = Vector3.zero;
        m_lastRootPos = m_rootBone.localPosition;
        m_lastAnimTime = 0;

        anim = this.GetComponent<Animation>();

        _cc = this.GetComponent<CharacterController>();
    }

    public Vector3 nextPos;


    void Update() 
	{

        if(Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Break();
        }
        //-- Apply Root Motion
        //nextPos = 

        //设置位置时调用CharacterController的Move方法，而不是直接设置transform.position,否则会无视物理和碰撞
        this.transform.position = this.transform.position + new Vector3(_两帧之间根骨骼本地坐标差值.x, 0, _两帧之间根骨骼本地坐标差值.z);
        if (_两帧之间根骨骼本地坐标差值.z >= 0.3f)
        {
            Debug.Log("update _两帧之间根骨骼本地坐标差值.z=" + _两帧之间根骨骼本地坐标差值.z);

        }

        //使人物的物理包围盒有Y轴上的位移
        _cc.center = new Vector3(_cc.center.x, _cc.center.y + _两帧之间根骨骼本地坐标差值.y / gameObject.transform.localScale.y + _cc.center.z);
        
        //用Character.Move会导致y值越来越大，人物越来越高
        //_cc.Move(new Vector3(_两帧之间根骨骼本地坐标差值.x, 0, _两帧之间根骨骼本地坐标差值.z));

        //-- 测试代码：更新摄像机
        //Camera.main.transform.LookAt(this.transform);
	}

    public void RecordRootPos()
    {
        //一个动作完成之后把此时的位置记录赋值，防止做完动作回到原先位置
        Debug.Log("RecordRootPos , m_lastRootPos.z=" + m_lastRootPos.z + ", m_rootBone.localPosition.z=" + m_rootBone.localPosition.z);

        m_lastRootPos = m_rootBone.localPosition;
        _两帧之间根骨骼本地坐标差值 = Vector3.zero;
        m_rootBone.localPosition = Vector3.zero;

        Debug.Log("after RecordRootPos , m_lastRootPos.z=" + m_lastRootPos.z + ", m_rootBone.localPosition.z=" + m_rootBone.localPosition.z);
    }


	void LateUpdate()
    {

        //return;
        TestAttack t = this.GetComponent<TestAttack>();

        for (int i = 0; i < m_AnimNameList.Count; i++)
        {
            string animName = m_AnimNameList[i];
            AnimationState animState = anim[animName];


            if (anim.IsPlaying(animName) && t._AnimState != null)
            //if (anim.IsPlaying(animName))
            {
                //Debug.Log("animState.normalizedTime=" + animState.normalizedTime + ", m_lastAnimTime=" + m_lastAnimTime);
                //Debug.Log("t._AnimState.time=" + t._AnimState.time + ", _AnimState.length=" + t._AnimState.length + ", Time.deltaTime=" + Time.deltaTime+", total="+(t._AnimState.time+Time.deltaTime));
                //Debug.Log("animState.time=" + animState.time + ", animState.length=" + animState.length);

                //if ((int)t._AnimState.normalizedTime > m_lastAnimTime)
                //if(animState.time >= animState.length - 0.01f)
                if(t._AnimState.time  >= t._AnimState.length)
                {
                    
                    //-- 动画循环处理
                   
                    RecordRootPos();

                    Debug.Log("animState.normalizedTime > m_lastAnimTime");
                }
                else
                {
                    //-- 计算当前帧的Root Motion
                    Vector3 rootPos = m_rootBone.localPosition;
                    if (m_lastRootPos.z == 0)
                    {
                        //动作做完后的一帧m_rootBone.localPosition.z会突然往前有一个位移，而此时m_lastRootPos.z还是0，导致坐标差很大会瞬移，
                        //这里抹平这个位移
                        m_lastRootPos = m_rootBone.localPosition;
                    }

                    _两帧之间根骨骼本地坐标差值 = rootPos - m_lastRootPos;



                    //如果模型放缩过，差值要乘以scale系数，才是正确的坐标差值
                    _两帧之间根骨骼本地坐标差值.x *= gameObject.transform.localScale.x;
                    _两帧之间根骨骼本地坐标差值.y *= gameObject.transform.localScale.y;
                    _两帧之间根骨骼本地坐标差值.z *= gameObject.transform.localScale.z;

                    Debug.Log("m_lastRootPos.z=" + m_lastRootPos.z + ", curRootPos.z=" + m_rootBone.localPosition.z);

                    m_lastRootPos = rootPos;
                    rootPos.x = 0;
                    rootPos.z = 0;

                    if (_两帧之间根骨骼本地坐标差值.y > 0.7f)
                    {
                        Debug.Log("_两帧之间根骨骼本地坐标差值.y =" + _两帧之间根骨骼本地坐标差值.y);
                    }

                    //_两帧之间根骨骼本地坐标差值.y = 0;


                    m_rootBone.localPosition = rootPos;
                }
                m_lastAnimTime = (int)t._AnimState.normalizedTime;

                return;
            }
        }
    }
}
